package com.wooophone.common.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * 一个专门用来打印HTTP头信息的过滤器
 * 
 * @author MingliC
 * 
 */
public class ShowHttpHeaderFilter implements Filter {

	private static final Logger log = Logger.getLogger(ShowHttpHeaderFilter.class);

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		try {

			HttpServletRequest httpServletRequest = (HttpServletRequest) request;

			Enumeration<?> enumeration = httpServletRequest.getHeaderNames();

			int i = 0;
			/* 遍历并打印头信息 */
			while (enumeration.hasMoreElements()) {
				Object object = (Object) enumeration.nextElement();
				log.debug("http header " + i + "---->" + object + ":"
						+ httpServletRequest.getHeader(object.toString()));
				i++;
			}

			log.debug("request url ---->" + httpServletRequest.getRequestURI());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			chain.doFilter(request, response);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}
}
